/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.entitygen.createvalassmethod;

import com.inspur.edp.caf.generator.Skippable;
import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.designtime.api.operation.CommonValidation;
import com.inspur.edp.cef.entity.dependenceTemp.DataValidator;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.EntityImplGenContext;
import com.inspur.edp.cef.generator.overall.GenUtils;
import com.inspur.edp.cef.generator.overall.GeneratorUtil;
import com.inspur.edp.cef.generator.repository.RepositoryGenUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.TypeLiteral;

public abstract class EntityCreateValAssMethodGenerator extends ClassMethodGenerator implements
    Skippable {

  private EntityImplGenContext entityImplGenContext;
  private String privateNodeCode;
  private static String varNameAssembler = "assembler";

  protected EntityCreateValAssMethodGenerator(EntityImplGenContext entityImplGenContext,
      String nodeCode) {
    DataValidator.checkForNullReference(entityImplGenContext, "entityCtx");
    DataValidator.checkForNullReference(nodeCode, "nodeCode");

    privateNodeCode = nodeCode;
    this.entityImplGenContext = entityImplGenContext;
  }

  protected final EntityImplGenContext getEntityImplGenContext() {
    return entityImplGenContext;
  }

  protected final String getNodeCode() {
    return privateNodeCode;
  }

  @Override
  protected TypeInfo getReturnType() {
    return new TypeInfo("void");
  }

  @Override
  protected Block buildMethodBody() {
    Block block = ast.newBlock();
    addDeterminationStatements(block);
    addExtendingStatements(block);
    return block;
  }

  protected void addExtendingStatements(Block block) {
  }

  private void addDeterminationStatements(Block block) {
    List<CommonValidation> types = getValMediateTypes();
    for (CommonValidation type : types) {
      TypeInfo adaptorType = getNewAdaptor_TypeParam(type);
      addImport(adaptorType);
      ClassInstanceCreation adaptorInstanceCreation = GenUtils
          .buildClassInstantiation(ast, adaptorType.getTypeName());
      buildNewAdaptorParameters(type, adaptorInstanceCreation.arguments());

      MethodInvocation methodInvocation = RepositoryGenUtil
          .createMethodInvocation(ast, ast.newSimpleName(varNameAssembler), "addValidations", null);
      methodInvocation.arguments().add(adaptorInstanceCreation);
      block.statements().add(ast.newExpressionStatement(methodInvocation));
    }
  }

  @Override
  public boolean skip() {
    return getValMediateTypes().isEmpty();
  }

  protected abstract List<CommonValidation> getValMediateTypes();

  protected abstract TypeInfo getValAssClassType();

  @Override
  protected boolean getIsOverride() {
    return true;
  }

  @Override
  protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
    ArrayList<Modifier.ModifierKeyword> list = new ArrayList<>();
    list.add(Modifier.ModifierKeyword.PROTECTED_KEYWORD);
    return list;
  }

  @Override
  protected ArrayList<ParameterInfo> getParameterCollection() {
    return new ArrayList<>(
        Arrays.asList(GeneratorUtil.createParameterInfo(getValAssClassType(), varNameAssembler)));
  }

  protected void buildNewAdaptorParameters(CommonValidation val, List args) {
    TypeLiteral typeLiteral = ast.newTypeLiteral();
    TypeInfo typeParam = getNewAdaptor_TypeParam(val);
    addImport(typeParam);
    typeLiteral.setType(ast.newSimpleType(ast.newName(GenUtils.getCompMethod(val,
        entityImplGenContext.getParentContext().getProjContext().getCefGenContext())
        .getClassName())));
    args.add(typeLiteral);

    buildExtendNewAdaptorParameter(val, args);
  }

  protected void buildExtendNewAdaptorParameter(CommonValidation val, List args) {
  }

  protected abstract TypeInfo getNewAdaptor_TypeParam(
      CommonValidation type);
}
